package com.cysion.study.presenter

import com.cysion.study.ktbox.base.BasePresenter
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.targetfun._subscribe
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.iview.LoginView

/**
 * Created by chu on 2019-06-30.
 */
class LoginPresenter : BasePresenter<LoginView>() {
    fun login(account: String, pwd: String, type: String) {
        checkViewAttached()
        attchedView?.loading()
        ApiClient.api.logins(account,pwd,type).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    attchedView?.stopLoad()
                    UserCache.saveToken(it.token)
                    attchedView?.setUserInfo(it)
                }
                _onError {
                    attchedView?.stopLoad()
                    error(it)
                }
            }
    }

}